program bmfconv;

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils,
  StrUtils,
  mrt_string;

var
  ifn, ofn : String;
  fi : TextFile;
  fo : File;
  buf : String;
  s1, s2, s3 : String;
  num : Integer;

function ReplaceSub(str, sub1, sub2 : String) : String;
var
  aPos: Integer;
  rslt: String;
begin
  aPos := Pos(sub1, str);
  rslt := '';
  while (aPos <> 0) do
  begin
    rslt := rslt + Copy(str, 1, aPos - 1) + sub2;
    Delete(str, 1, aPos + Length(sub1) - 1);
    aPos := Pos(sub1, str);
  end;
  Result := rslt + str;
end;

begin
  ifn := ParamStr(1);

  if (ifn = '') or (not FileExists(ifn)) then
  begin
    WriteLn('MBFont Convetter');
    WriteLn('(c) Osokin Makar, 2007'#13#10);
    WriteLn('Example: pmfconv.exe input_file_name [output_file_name]');
    ReadLn;
    Exit;
  end;

  ofn := ParamStr(2);
  if ofn = '' then
  begin
    ofn := ReplaceSub(ifn, '.fnt', '.bmf');
  end;

  WriteLn('Convert "'+ifn+'" to "'+ofn+'".');
  
  AssignFile(fi, ifn);
  Reset(fi);
  AssignFile(fo, ofn);
  Rewrite(fo, 1);
  
  while not EOF(fi) do
  begin
    ReadLn(fi, buf);
    buf := trim(buf);
    buf := ReplaceSub(buf, '  ', ' ');
    buf := ReplaceSub(buf, '  ', ' ');
    buf := ReplaceSub(buf, '  ', ' ');
    buf := ReplaceSub(buf, '  ', ' ');
    buf := ReplaceSub(buf, '  ', ' ');
    s1 := LowerCase(str_split(' ', buf));
    if s1 = 'info' then
    begin
    
    end
    else if s1 = 'common' then
    begin
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // lineheight
      s2 := LowerCase(str_split(' ', buf));
      s2 := LowerCase(str_split(' ', buf));
      s2 := LowerCase(str_split(' ', buf));
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // pages
    end
    else if s1 = 'page' then
    begin
      s2 := LowerCase(str_split(' ', buf));
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      s2 := copy(s2, 2, Length(s2)-2);
      num := Length(s2);
      BlockWrite(fo, num, 4);
      BlockWrite(fo, s2[1], num); // file
    end
    else if s1 = 'chars' then
    begin
      s3 := LowerCase(str_split('=', buf));
      num := StrToInt(buf);
      BlockWrite(fo, num, 4); // count
      WriteLn('chars count='+buf);
    end
    else if s1 = 'char' then
    begin
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // id
      //WriteLn('id='+s2);
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // x
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // y
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // width
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // height
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // xoffset
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // yoffset
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // xadvance
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // page
      //WriteLn('page='+s2);
      
      s2 := LowerCase(str_split(' ', buf));
      s3 := LowerCase(str_split('=', s2));
      num := StrToInt(s2);
      BlockWrite(fo, num, 4); // chnl
      
      //WriteLn;
    end;
  end;
  
  CloseFile(fo);
  CloseFile(fi);
  
  WriteLn('End.');

end.
